<script setup>
import {
  Edit,
  Delete
} from '@element-plus/icons-vue'

import {ref} from 'vue'
// import {articleAddService, articleCategoryListService, articleListService} from "@/api/article";


//用户搜索时选中的分类id
const indexId = ref('')
const orderName = ref('aa')
//用户搜索时选中的发布状态
const state = ref()

//文章列表数据模型
const orderInfo = ref([
  {'id':1,'name':'三星固态硬盘','img':'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg','num':2,'money':100,'state':1,'commentId':''}
])

//分页条数据模型
const pageNum = ref(1)//当前页
const total = ref(20)//总条数
const pageSize = ref(7)//每页条数

//当每页条数发生了变化，调用此函数
const onSizeChange = (size) => {
  pageSize.value = size
  articleList()
}
//当前页码发生变化，调用此函数
const onCurrentChange = (num) => {
  pageNum.value = num
  articleList()
}
//回显文章分类
// const articleCategoryList = async () => {
//   let result = await articleCategoryListService()
//   categorys.value = result.data
// }
// articleCategoryList()
//获取文章列表数据
const articleList =  () => {
  console.log(indexId.value)
  console.log(state.value)
  // let params = {
  //   pageNum: pageNum.value,
  //   pageSize: pageSize.value,
  //   categoryId: categoryId.value ? categoryId.value : null,
  //   state: state.value ? state.value : null
  // }
  // let result = await articleListService(params)
  // //渲染视图
  // total.value = result.data.total
  // articles.value = result.data.items
  //
  // //处理数据，给数据模型扩展一个属性categoryName,分类名称
  // for (let i = 0; i < articles.value.length; i++) {
  //   let article = articles.value[i]
  //   for (let j = 0; j < categorys.value.length; j++) {
  //     if (article.categoryId === categorys.value[j].id) {
  //       article.categoryName = categorys.value[j].categoryName
  //     }
  //   }
  // }
}
articleList()

import {QuillEditor} from '@vueup/vue-quill'
import '@vueup/vue-quill/dist/vue-quill.snow.css'
import {Plus} from '@element-plus/icons-vue'
//控制抽屉是否显示
const visibleDrawer = ref(false)
//添加表单数据模型
const orderModel = ref({
  rate:ref(),
  coverImg:'',
  content:ref('')
})

//导入token
import {useTokenSotre} from '@/stores/token.js'
import {ElMessage, ElMessageBox} from "element-plus";
const tokenStore =useTokenSotre()

//上传成功的回调函数
const uploadSuccess=(result)=>{
  articleModel.value.coverImg = result.data
}

//添加文章
// const addArticle = async (states)=>{
//   articleModel.value.state = states
//   let result = await articleAddService(articleModel.value)
//   ElMessage.success(result.meg?result.msg:'添加成功')
//   visibleDrawer.value=false
//   //刷新列表
//   articleList()
// }
//评分
const colors = ref(['#99A9BF', '#F7BA2A', '#FF9900'])

const dialogImageUrl = ref('')
const dialogVisible = ref(false)
import  { UploadProps } from 'element-plus'
const handleRemove = (uploadFile, uploadFiles) => {
  console.log(uploadFile, uploadFiles)
}

const handlePictureCardPreview= (uploadFile) => {
  dialogImageUrl.value = !uploadFile.url
      dialogVisible.value = true
}


//跳转商品详情页面
const toProducts=(name)=>{
  console.log(name)
}
</script>
<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>订单管理</span>
      </div>
    </template>
    <!-- 搜索表单 -->
    <el-form inline>
      <el-form-item label="商品分类：">
        <el-select placeholder="请选择" v-model="indexId" style="width: 200px">
          <el-option label="cpu" value=0></el-option>
          <el-option label="gpu" value=1></el-option>
          <el-option label="主板" value=2></el-option>
          <el-option label="内存条" value=3></el-option>
          <el-option label="硬盘" value=4></el-option>
          <el-option label="电源" value=5></el-option>
          <el-option label="cpu散热器" value=6></el-option>
          <el-option label="机箱" value=7></el-option>
          <el-option label="显示器" value=8></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="订单状态：" style="width: 180px">
        <el-select placeholder="请选择" v-model="state">
          <el-option label="待支付" value=0></el-option>
          <el-option label="已支付" value=1></el-option>
          <el-option label="已发货" value=2></el-option>
          <el-option label="已完成" value=3></el-option>
          <el-option label="售后中" value=4></el-option>
          <el-option label="已退货" value=5></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="articleList()">搜索</el-button>
        <el-button @click="categoryId='';state=''">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 文章列表 -->
    <el-table :data="orderInfo" style="width: 100%">
      <el-table-column label="订单编号" width="200" prop="id"></el-table-column>
      <el-table-column label="商品名称" prop="name"><template #default="{ row }">
        <div @click="toProducts(orderInfo[row.id-1].name)"><el-image style="width: 50px; height: 50px; " :src="orderInfo[row.id-1].img" :fit="fit" /><p style="text-align: center ;margin-top: -40px;">{{orderInfo[row.id-1].name}}</p></div>
      </template></el-table-column>
      <el-table-column label="数量" prop="num"></el-table-column>
      <el-table-column label="金额" prop="money"></el-table-column>
      <el-table-column label="状态" prop="state"></el-table-column>
      <el-table-column label="编写评论" width="100">
        <template #default="{ row }">
          <el-button :icon="Edit" circle plain type="primary" @click="visibleDrawer=true;orderName = row.name" :disabled="orderInfo[row.id-1].commentId!==''"></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据"/>
      </template>
    </el-table>
    <!-- 分页条 -->
    <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize" :page-sizes="[3, 5 ,10, 15]"
                   layout="jumper, total, sizes, prev, pager, next" background :total="total"
                   @size-change="onSizeChange"
                   @current-change="onCurrentChange" style="margin-top: 20px; justify-content: flex-end"/>
    <!-- 抽屉 -->
    <el-drawer v-model="visibleDrawer" title="编写评论（请确保评论内容真实）" direction="rtl" size="30%"><p>{{orderName}}</p>
      <!-- 添加文章表单 -->
      <el-form :model="articleModel" label-width="100px">
        <el-form-item label="商品评分">
          <el-rate v-model="orderModel.rate" :colors="colors" />
        </el-form-item>
        <el-form-item label="商品图片">
<!--          <el-upload class="avatar-uploader" :auto-upload="true" :show-file-list="false" action="http://localhost:8080/upload" name="file" :headers="{'Authorization':tokenStore.token}"-->
<!--                     :on-success='uploadSuccess'>-->
<!--            <img v-if="orderModel.coverImg" :src="articleModel.coverImg" class="avatar"/>-->
<!--            <el-icon v-else class="avatar-uploader-icon">-->
<!--              <Plus/>-->
<!--            </el-icon>-->
<!--          </el-upload>-->
          <el-upload
              class="avatar-uploader"
              :auto-upload="true"
              v-model:file-list="fileList"
              action="http://localhost:8080/upload" name="file" :headers="{'Authorization':tokenStore.token}"
              list-type="picture-card"
              :on-preview="handlePictureCardPreview"
              :on-remove="handleRemove"
          >
            <el-icon><Plus /></el-icon>
          </el-upload>
        </el-form-item>
        <el-form-item label="评论内容">
          <div class="editor">
            <el-input
                v-model="orderModel.content"
                style="width: 300px"
                :autosize="{ minRows: 2, maxRows: 4 }"
                type="textarea"
                placeholder="请输入评论内容"
            />
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addArticle('已发布')">发布</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </el-card>
</template>
<style lang="scss" scoped>
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}

/* 抽屉样式 */
.avatar-uploader {
    .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }

    .el-upload {
      border: 1px dashed var(--el-border-color);
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      transition: var(--el-transition-duration-fast);
    }

    .el-upload:hover {
      border-color: var(--el-color-primary);
    }

    .el-icon.avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      text-align: center;
    }
}

.editor {
  width: 100%;

  :deep(.ql-editor) {
    min-height: 200px;
  }
}
</style>